﻿using System;
using System.IO;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;

namespace PipeTest.Win32
{
    public static class NativeMethod
    {
        public const int KEYEVENTF_EXTENDEDKEY = 0x01;
        public const int KEYEVENTF_KEYUP = 0x02;

        [DllImport("user32.dll")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);

        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern SafeFileHandle CreateFile(string lpFileName, int dwDesiredAccess, FileShare dwShareMode,
                                                        SecurityAttributes securityAttrs, FileMode dwCreationDisposition,
                                                        int dwFlagsAndAttributes, IntPtr hTemplateFile);

        [DllImport("kernel32.dll", SetLastError = true)]
        public static extern unsafe int WriteFile(SafeFileHandle handle, byte* bytes, int numBytesToWrite,
                                                    out int numBytesWritten, IntPtr mustBeZero);

        [DllImport("kernel32.dll", SetLastError = true)]
        public static extern int WaitNamedPipe(string lpNamedPipeName, int nTimeOut);

        [DllImport("kernel32.dll", SetLastError = true)]
        public static extern int CloseHandle(IntPtr hObject);

        [DllImport("user32.dll", SetLastError = true)]
        public static extern int MapVirtualKey(int uCode, int uMapType);

        [DllImport("user32.dll")]
        public static extern int ShowWindow(IntPtr hWnd, int nCmdShow);

       [DllImport("user32.dll")]
        public static extern int BringWindowToTop(IntPtr hWnd);

       [DllImport("user32.dll")]
       public static extern void keybd_event(byte bVk, byte bScan, uint dwFlags,
          int dwExtraInfo);
    }
}
